//
//  String+Extension.swift
//  WB
//
//  Created by 汪鹏 on 2017/1/19.
//  Copyright © 2017年 汪鹏. All rights reserved.
//  用正则匹配链接、字符串

import Foundation

extension String {
    
    /// oc可以用字典、数组、自定对象、指针的指针返回多个值，swift提供了元组返回多个值
    func regularHrefAndText() -> (txt:String?, href:String?)? {
        
         // 匹配规则
        let pattern = "<a href=\"(.*?)\".*?>(.*?)</a>"
        
        guard let regular = try? NSRegularExpression(pattern: pattern, options: []),
            
        let result = regular.firstMatch(in: self, options: [], range: NSMakeRange(0, self.characters.count))
            
            else{
                
            return nil
        }
        
        let href = (self as NSString).substring(with: result.rangeAt(1))
        
        let txt = (self as NSString).substring(with: result.rangeAt(2))
        
        return (txt, href)
        
    }
    
    
}
